package com.pengtu.service.business;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.jbpm.api.task.Task;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.pengtu.Constants;
import com.pengtu.dao.Page;
import com.pengtu.dao.QueryFilter;
import com.pengtu.dao.business.FlowLabsFilingDao;
import com.pengtu.dao.business.LabsAuthorizationDao;
import com.pengtu.dao.business.LabsNoticeDao;
import com.pengtu.dao.business.LabsPetitionDao;
import com.pengtu.dao.history.HistoryProjectDao;
import com.pengtu.entity.app.User;
import com.pengtu.entity.business.labs.FlowLabsFiling;
import com.pengtu.entity.business.labs.LabsAuthorization;
import com.pengtu.entity.business.labs.LabsNotice;
import com.pengtu.entity.business.labs.LabsPetition;
import com.pengtu.entity.history.HistoryProject;
import com.pengtu.entity.project.ProjectInfo;
import com.pengtu.service.BaseManager;

/**
 * 实验室授权相关实体的管理类
 * 
 * @author liuyang
 * 
 */
@Component
@Transactional
public class LabsFilingManager extends BaseManager {
	private FlowLabsFilingDao flowLabsFilingDao;
	private LabsAuthorizationDao labsAuthorizationDao;
	private LabsNoticeDao labsNticeDao;
	private LabsPetitionDao labsPetitionDao;
	private HistoryProjectDao historyProjectDao;
	
	@Autowired
	public void setHistoryProjectDao(HistoryProjectDao historyProjectDao) {
		this.historyProjectDao = historyProjectDao;
	}

	@Autowired
	public void setFlowLabsFilingDao(FlowLabsFilingDao flowLabsFilingDao) {
		this.flowLabsFilingDao = flowLabsFilingDao;
	}

	@Autowired
	public void setLabsAuthorizationDao(
			LabsAuthorizationDao labsAuthorizationDao) {
		this.labsAuthorizationDao = labsAuthorizationDao;
	}

	@Autowired
	public void setLabsNticeDao(LabsNoticeDao labsNticeDao) {
		this.labsNticeDao = labsNticeDao;
	}

	/**
	 * @param labsPetitionDao
	 *            the labsPetitionDao to set
	 */
	@Autowired
	public void setLabsPetitionDao(LabsPetitionDao labsPetitionDao) {
		this.labsPetitionDao = labsPetitionDao;
	}

	/**
	 * 保存
	 * 
	 * @param flowlabsFiling
	 */
	public void save(FlowLabsFiling flowLabsFiling) {
		flowLabsFilingDao.save(flowLabsFiling);
	}

	public void save(LabsAuthorization labsAuthorization) {
		labsAuthorizationDao.save(labsAuthorization);
	}

	public void save(LabsPetition labsPetition) {
		labsPetitionDao.save(labsPetition);
	}

	/**
	 * 保存流程表和实验室申请表 save: 适用:
	 * 
	 * @param labsAuthorization
	 * @param flowLabsFiling
	 * @exception
	 * @since 1.0.0
	 */
	public void save(LabsAuthorization labsAuthorization,
			FlowLabsFiling flowLabsFiling) {
		flowLabsFilingDao.save(flowLabsFiling);
		labsAuthorization.setFlowLabsFiling(flowLabsFiling);
		labsAuthorizationDao.save(labsAuthorization);
	}
	
	public void save(FlowLabsFiling flowLabsFiling,LabsPetition labsPetition){
		flowLabsFilingDao.save(flowLabsFiling);
		labsPetition.setFlowLabsFiling(flowLabsFiling);
		labsPetitionDao.save(labsPetition);
	}

	public void save(LabsNotice labsNotice) {
		labsNticeDao.save(labsNotice);
	}

	/**
	 * 查询
	 * 
	 * @param id
	 * @return
	 */
	public FlowLabsFiling getFlowLabsFiling(String id) {
		return flowLabsFilingDao.get(id);
	}

	public Page<FlowLabsFiling> getFlowLabsFilings(
			final Page<FlowLabsFiling> page, final QueryFilter queryFilter) {
		return flowLabsFilingDao.findPage(page, queryFilter);
	}

	public List<FlowLabsFiling> getFlowLabsFiling(ProjectInfo projectInfo) {
		return flowLabsFilingDao.findBy("projectInfo", projectInfo);
	}

	public LabsAuthorization getLabsAuthorization(String id) {
		return labsAuthorizationDao.get(id);
	}

	public LabsNotice getLabsNotice(String id) {
		return labsNticeDao.get(id);
	}

	public LabsAuthorization getLabsAuthorization(FlowLabsFiling flowLabsFiling) {
		return labsAuthorizationDao.findUniqueBy("flowLabsFiling",
				flowLabsFiling);
	}

	public LabsNotice getLabsNotice(FlowLabsFiling flowLabsFiling) {
		return labsNticeDao.findUniqueBy("flowLabsFiling", flowLabsFiling);
	}

	public List<LabsAuthorization> getLabsAuthorization(ProjectInfo projectInfo) {
		return labsAuthorizationDao.findBy("pid", projectInfo.getId());
	}

	public LabsPetition getLabsPetition(String id) {
		return labsPetitionDao.get(id);
	}

	public LabsPetition getLabsPetition(FlowLabsFiling flowLabsFiling) {
		return labsPetitionDao.findUniqueBy("flowLabsFiling", flowLabsFiling);
	}

	/**
	 * 待办任务 findPersonalTasks: 适用:流程处理
	 * 
	 * @param user
	 * @param projectInfo
	 * @return
	 * @exception
	 * @since 1.0.0
	 */
	public List<FlowLabsFiling> findPersonalTasks(User user,
			ProjectInfo projectInfo) {
		List<Task> taskList = super.findPersonalTasks(user);
		List<FlowLabsFiling> flowLabsFilings = new ArrayList<FlowLabsFiling>();
		for (Task task : taskList) {
			String taskId = task.getId();
			String exectionId = task.getExecutionId();
			String arg = exectionId.substring(0, 4);
			if ("labs".equals(arg)) {
				String id = (String) super.getVariableByTaskId(taskId,
						Constants.LABS_ID);
				FlowLabsFiling flowLabsFiling = flowLabsFilingDao.get(id);
				if (flowLabsFiling != null) {
					if (StringUtils.equals(flowLabsFiling.getProjectInfo()
							.getId(), projectInfo.getId())) {
						flowLabsFiling.setTaskId(taskId);
						flowLabsFilings.add(flowLabsFiling);
					}
				}
			}
		}
		return flowLabsFilings;
	}

	public void save(FlowLabsFiling entity, HistoryProject historyProject) {
		flowLabsFilingDao.save(entity);
		historyProjectDao.save(historyProject);
	}

}
